﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TbShop.Web.Areas.Admin.Models
{
    /// <summary>
    /// 验证码验证对象
    /// </summary>
    public class ValidationCodeFactory
    {
        public ValidationCodeFactory()
        {
            List = new List<ValidationCodeEntity>();
        }

        /// <summary>
        /// 记录验证码
        /// </summary>
        /// <param name="code">验证码</param>
        /// <param name="type">验证码类别</param>
        /// <returns>是否记录成功</returns>
        public bool SetCode(string phone, string code, string type)
        {
            List.Add(new ValidationCodeEntity
            {
                RequestTime = DateTime.Now,
                Code = code,
                State = ValidationState.Request,
                Type = type,
                Phone = phone
            });
            return true;
        }

        /// <summary>
        /// 校验验证码
        /// </summary>
        /// <param name="code">验证码类别</param>
        /// <param name="type">待验证的验证码</param>
        /// <returns>验证结果</returns>
        public ValidationResult ValidateCode(string phone, string code, string type)
        {
            var now = DateTime.Now;
            var findCode = List.Where(x => x.Type == type && x.Phone == phone).OrderByDescending(x => x.RequestTime).FirstOrDefault();
            if (findCode == null)
            {
                //没有该类别的验证码
                return ValidationResult.NoneRequest;
            }
            else if (findCode.RequestTime.Add(TimeSpan.FromMinutes(5)) < now || findCode.State == ValidationState.Timeout)
            {
                //TODO 超时时间配置化
                //已经超时
                findCode.State = ValidationState.Timeout;
                return ValidationResult.Timeout;
            }
            else if (findCode.State == ValidationState.Checked)
            {
                //验证码已被验证
                return ValidationResult.Checked;
            }
            else if (findCode.State == ValidationState.Request)
            {
                //验证码状态正常
                if (findCode.Code == code)
                {
                    return ValidationResult.Success;
                }
                else
                {
                    return ValidationResult.Wrong;
                }
            }
            else
            {
                //验证码状态未知
                return ValidationResult.OtherError;
            }
        }

        public void SetCodeChecked(string phone, string code, string type)
        {
            var findCode = List.Where(x => x.Type == type && x.Code == code && x.Phone == phone).OrderByDescending(x => x.RequestTime).FirstOrDefault();
            if (findCode != null)
            {
                findCode.State = ValidationState.Checked;
            }
        }

        /// <summary>
        /// 验证码列表
        /// </summary>
        List<ValidationCodeEntity> List { get; set; }
    }

    /// <summary>
    /// 验证码对象
    /// </summary>
    public class ValidationCodeEntity
    {
        /// <summary>
        /// 验证码类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 生成时间
        /// </summary>
        public DateTime RequestTime { get; set; }

        /// <summary>
        /// 验证码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 对应的手机号码
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public ValidationState State { get; set; }
    }

    /// <summary>
    /// 当前验证码的状态
    /// </summary>
    public enum ValidationState
    {
        /// <summary>
        /// 已生成
        /// </summary>
        Request,
        /// <summary>
        /// 已验证
        /// </summary>
        Checked,
        /// <summary>
        /// 已过期
        /// </summary>
        Timeout
    }

    /// <summary>
    /// 验证结果
    /// </summary>
    public enum ValidationResult
    {
        /// <summary>
        /// 验证成功
        /// </summary>
        Success,
        /// <summary>
        /// 验证码错误
        /// </summary>
        Wrong,
        /// <summary>
        /// 验证码超时
        /// </summary>
        Timeout,
        /// <summary>
        /// 未找到验证信息
        /// </summary>
        NoneRequest,
        /// <summary>
        /// 验证码已被验证过
        /// </summary>
        Checked,
        /// <summary>
        /// 其他错误
        /// </summary>
        OtherError
    }
}